package com.heu.blood.autologous.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heu.blood.application.dictenum.ApplicationStateEnum;
import com.heu.blood.autologous.dao.BloodApplicationAutologousDao;
import com.heu.blood.autologous.dao.BloodApplicationItemAutologousDao;
import com.heu.blood.autologous.dao.BloodCollectAutologousDao;
import com.heu.blood.autologous.dao.BloodInventoryAutologousDao;
import com.heu.blood.autologous.dictenum.ApplicationStateAutologousEnum;
import com.heu.blood.autologous.dictenum.CollectStateEnum;
import com.heu.blood.autologous.dictenum.InventoryStateAutologousEnum;
import com.heu.blood.autologous.entity.BloodApplicationAutologousEntity;
import com.heu.blood.autologous.entity.BloodApplicationItemAutologousEntity;
import com.heu.blood.autologous.entity.BloodCollectAutologousEntity;
import com.heu.blood.autologous.entity.BloodInventoryAutologousEntity;
import com.heu.blood.autologous.service.BloodApplicationAutologousService;
import com.heu.blood.autologous.service.BloodApplicationItemAutologousService;
import com.heu.blood.autologous.service.BloodCollectAutologousService;
import com.heu.blood.common.Constant.ConstantData;
import com.heu.blood.common.enums.BloodApplicationAutologousEnum;
import com.heu.blood.common.enums.BloodApplicationItemAutologousEnum;
import com.heu.blood.common.utils.GenerateNumberBySnowFlake;
import com.heu.blood.common.utils.PageUtils;
import com.heu.blood.common.utils.Query;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;


@Service("bloodCollectAutologousService")
public class BloodCollectAutologousServiceImpl extends ServiceImpl<BloodCollectAutologousDao, BloodCollectAutologousEntity> implements BloodCollectAutologousService {

    @Autowired
    private BloodApplicationItemAutologousService bloodApplicationItemAutologousService;

    @Autowired
    private BloodApplicationAutologousService bloodApplicationAutologousService;

    @Autowired
    private BloodApplicationAutologousDao bloodApplicationAutologousDao;

    @Autowired
    private BloodApplicationItemAutologousDao bloodApplicationItemAutologousDao;

    @Autowired
    private BloodInventoryAutologousDao bloodInventoryAutologousDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<BloodCollectAutologousEntity> page = this.page(
                new Query<BloodCollectAutologousEntity>().getPage(params),
                new QueryWrapper<BloodCollectAutologousEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    @Transactional
    public int savebloodcollect(BloodCollectAutologousEntity bloodCollectAutologous) {
        bloodCollectAutologous.setCollectState(CollectStateEnum.ITEM_BLOOD_COLLECTED.code());
        this.saveOrUpdate(bloodCollectAutologous);
        Long applicationItemId = bloodCollectAutologous.getBloodApplicationItemAutologousId();
        String applicationId = bloodCollectAutologous.getApplicationId();
        if (applicationItemId == null || applicationId == null) {
            return 1;
        }
        BloodApplicationItemAutologousEntity bloodApplicationItemAutologousEntity = new BloodApplicationItemAutologousEntity();
        bloodApplicationItemAutologousEntity.setId(bloodCollectAutologous.getBloodApplicationItemAutologousId());
        bloodApplicationItemAutologousEntity.setIfCollected(Integer.parseInt(BloodApplicationItemAutologousEnum.COLLECTED.code()));
        bloodApplicationItemAutologousEntity.setCollectState(CollectStateEnum.ITEM_BLOOD_COLLECTED.code());
        bloodApplicationItemAutologousService.updateById(bloodApplicationItemAutologousEntity);

        //判断本申请项采完血后是否对应申请单的全部申请项都已采完血
        LambdaQueryWrapper<BloodApplicationAutologousEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BloodApplicationAutologousEntity::getApplicationId,applicationId);
        BloodApplicationAutologousEntity bloodApplicationAutologousEntity = bloodApplicationAutologousDao.selectOne(queryWrapper);

        LambdaQueryWrapper<BloodApplicationItemAutologousEntity> queryWrapper1 = new LambdaQueryWrapper<>();
        queryWrapper1.eq(Strings.isNotBlank(applicationId), BloodApplicationItemAutologousEntity::getApplicationId, applicationId);
        List<BloodApplicationItemAutologousEntity> bloodApplicationItemAutologousEntityList = bloodApplicationItemAutologousDao.selectList(queryWrapper1);

        int f = 0;
        for (BloodApplicationItemAutologousEntity b : bloodApplicationItemAutologousEntityList) {
            String collectState = b.getCollectState();
            if (!CollectStateEnum.ITEM_BLOOD_COLLECTED.code().equals(collectState)) {
                f = 1;
                break;
            }
        }


        if (f == 0) {
            bloodApplicationAutologousEntity.setApplicationState(ApplicationStateAutologousEnum.BLOOD_COLLECTED.code());
            bloodApplicationAutologousDao.updateById(bloodApplicationAutologousEntity);
        }

        // 生成一条库存表记录
        BloodInventoryAutologousEntity bloodInventoryAutologousEntity = new BloodInventoryAutologousEntity();
        BeanUtils.copyProperties(bloodApplicationAutologousEntity, bloodInventoryAutologousEntity);

        GenerateNumberBySnowFlake generateNumberBySnowFlake = new GenerateNumberBySnowFlake(ConstantData.AUTOLOGOUS_BLOOD_NO_WORK_ID, ConstantData.AUTOLOGOUS_BLOOD_NO_PREFIX);
        String bloodId = generateNumberBySnowFlake.genNumberByflag();

        bloodInventoryAutologousEntity.setBloodId(bloodId);
        bloodInventoryAutologousEntity.setGmtCreate(new Date());
        bloodInventoryAutologousEntity.setGmtModified(new Date());
        bloodInventoryAutologousEntity.setTransferStatus(InventoryStateAutologousEnum.AUTO_PENDING_RECEIPT.code());
        bloodInventoryAutologousEntity.setOperatorId("");
        bloodInventoryAutologousEntity.setOperatorName("");
        bloodInventoryAutologousEntity.setNotes("");
        bloodInventoryAutologousEntity.setTransfusionTime(bloodCollectAutologous.getTransfusionTime());
        bloodInventoryAutologousEntity.setBloodType(bloodCollectAutologous.getBloodType());
        bloodInventoryAutologousEntity.setBloodCount(bloodCollectAutologous.getApplyCount());
        bloodInventoryAutologousEntity.setMeasurementUnit(bloodCollectAutologous.getMeasurementUnit());
        bloodInventoryAutologousEntity.setBloodApplicationItemId(bloodCollectAutologous.getBloodApplicationItemAutologousId());


        bloodInventoryAutologousDao.insert(bloodInventoryAutologousEntity);



        return 0;
    }
}